package com.jieshuibao.common.config;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

/**
 * Created by FirenzesEagle on 2016/6/1 0001.
 * Email:liumingbo2008@gmail.com
 */
public class MenuConfig {

	static ApplicationContext ctx = new ClassPathXmlApplicationContext("com/jieshuibao/context/spring.xml");
    /**
     * 定义菜单结构
     *
     * @return
     */
    protected static WxMenu getMenu() {

//        MainConfig mainConfig = new MainConfig();
        
    	WxMpService wxMpService = ctx.getBean("wxMpService", WxMpServiceImpl.class);
    	System.out.println(wxMpService.getWxMpConfigStorage().getAppId());

        WxMenu menu = new WxMenu();
        WxMenuButton button1 = new WxMenuButton();
        button1.setName("首页");
        button1.setType(WxConsts.BUTTON_VIEW);
        button1.setUrl(wxMpService.oauth2buildAuthorizationUrl(Global.getConfig("wx.server.url")+"/jsb_weixin/html/test.html?icode=1", "snsapi_base", ""));
        
        
        
        WxMenuButton button2 = new WxMenuButton();
        button2.setName("财税圈");
        button2.setType(WxConsts.BUTTON_VIEW);
        button2.setUrl(wxMpService.oauth2buildAuthorizationUrl(Global.getConfig("wx.server.url")+"/jsb_weixin/html/test.html?icode=2", "snsapi_base", ""));

    

        WxMenuButton button3 = new WxMenuButton();
        button3.setName("我的");
        button3.setType(WxConsts.BUTTON_VIEW);
        button3.setUrl(wxMpService.oauth2buildAuthorizationUrl(Global.getConfig("wx.server.url")+"/jsb_weixin/html/test.html?icode=4", "snsapi_base", ""));
//        WxMenuButton button31 = new WxMenuButton();
//        button31.setType(WxConsts.BUTTON_VIEW);
//        button31.setName("我的二维码");
//        button31.setUrl(wxMpService.oauth2buildAuthorizationUrl("http://test.jieshuibao.com/jsbPage/html/infoJsb.html", "snsapi_base", ""));
        
//        button3.getSubButtons().add(button31);

        menu.getButtons().add(button1);
        menu.getButtons().add(button2);
        menu.getButtons().add(button3);

        return menu;
    }

    /**
     * 运行此main函数即可生成公众号自定义菜单
     *
     * @param args
     */
    public static void main(String[] args) {
    	WxMpService wxMpService = ctx.getBean("wxMpService", WxMpServiceImpl.class);
    	System.out.println(wxMpService.getWxMpConfigStorage().getAppId());
        try {
            wxMpService.getMenuService().menuCreate(getMenu());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }

}
